package com.situ.company.employee.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.employee.service.EmployeeService;
import com.situ.company.util.FmtRequest;
import com.situ.company.util.Upload;

@WebServlet("/employee")
public class EmployeeServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    // 控制层： 接受请求 获取参数 封装对象 调用方法 返回结果

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        Object res = "";
        switch (action) {
            case "reg":
            case "add":
                res = reg(req);
                break;
            case "login":
                res = login(req);
                break;
            case "page":
                res = page(req);
                break;
            case "del":
                res = del(req);
                break;
            case "get":
                res = get(req);
                break;
            case "upd":
                res = upd(req);
                break;
            case "resetPass":
                res = resetPass(req);
                break;
            case "updPass":
                res = updPass(req);
                break;
            case "updDept":
                res = updDept(req);
                break;
            case "logout":
                req.getSession().removeAttribute("user");
                resp.sendRedirect(req.getContextPath() + "/web/login.jsp");
                return;
            case "delpic":
                res = delpic(req);
                break;
        }
        FmtRequest.write(resp.getWriter(), res);
    }

    private EmployeeService service = new EmployeeService();

    private String reg(HttpServletRequest req) {
        // String code = req.getParameter("code");
        // String name = req.getParameter("name");
        // String pass = req.getParameter("pass");
        // EmployeeModel model = new EmployeeModel(code, name, pass);
        EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class);
        return service.insert(model);
    }

    private String login(HttpServletRequest req) {
        // String code = req.getParameter("code");
        // String pass = req.getParameter("pass");
        // EmployeeModel model = new EmployeeModel(code, pass);
        EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class);
        String res = service.login(model);
        if ("1".equals(res))
            req.getSession().setAttribute("user", service.selectModel(model));
        return res;
    }

    private String del(HttpServletRequest req) {
        EmployeeModel user = (EmployeeModel) req.getSession().getAttribute("user");
        String code = req.getParameter("code");
        return user.getCode().equals(code) ? "no" : service.delete(new EmployeeModel(code));
    }

    private String upd(HttpServletRequest req) {
        Map<String, String> fields = new HashMap<>();
        fields.put("code", "code");
        fields.put("name", "name");
        EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class, fields);
        String res = service.update(model);
        EmployeeModel user = (EmployeeModel) req.getSession().getAttribute("user");
        if ("1".equals(res) && user.getCode().equals(model.getCode()))
            req.getSession().setAttribute("user", model);
        return res;
    }

    private String resetPass(HttpServletRequest req) {
        EmployeeModel model = new EmployeeModel(req.getParameter("code"));
        return service.updatePassReset(model);
    }

    private String updPass(HttpServletRequest req) {
        Map<String, String> fields = new HashMap<>();
        fields.put("code", "code");
        fields.put("pass", "pass");
        EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class, fields);
        return service.updatePass(model);
    }

    private String updDept(HttpServletRequest req) {
        Map<String, String> fields = new HashMap<>();
        fields.put("code", "code");
        fields.put("codeDept", "codeDept");
        EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class, fields);
        return service.updateDept(model);
    }

    private String delpic(HttpServletRequest req) {
        String code = req.getParameter("code");
        EmployeeModel model = new EmployeeModel(code);
        EmployeeModel dbm = service.selectModel(model);
        String image = dbm.getImage();
        Upload.delFile(image);
        dbm.setImage("");
        EmployeeModel modelSession = (EmployeeModel) req.getSession().getAttribute("user");
        if (modelSession.getCode().equals(code)) {
            req.getSession().setAttribute("user", dbm);
        }
        model.setImage("");
        return service.updateImage(model);
    }

    private EmployeeModel get(HttpServletRequest req) {
        EmployeeModel model = new EmployeeModel(req.getParameter("code"));
        return service.selectModel(model);
    }

    private Map<String, Object> page(HttpServletRequest req) {
        Map<String, String> fields = new HashMap<>();
        fields.put("code", "code");
        fields.put("name", "name");
        EmployeeModel model = FmtRequest.parseModel(req, EmployeeModel.class, fields);
        model.setPageIndex(Integer.parseInt(req.getParameter("pageIndex")));
        model.setPageLimit(Integer.parseInt(req.getParameter("pageLimit")));
        model.setPageOn(true);
        Map<String, Object> map = new HashMap<>();
        map.put("data", service.selectList(model));
        map.put("count", service.selectCount(model));
        return map;
    }
}
